SConscriptChdir(0)

import os
import platform

def RBConfig(param):
	return os.popen('ruby -e"require \'rbconfig\'; print Config::CONFIG[\'' + param + '\']"').read()


env = Environment(
	# GCC flags for building libchipmunk
	CCFLAGS = RBConfig('CFLAGS').split() + ['-O3', '-ffast-math', '-std=gnu99'],
	CPPPATH = ['../include/chipmunk', RBConfig('topdir')],
)

if(platform.system() == "Darwin"):
	SDK = "/Developer/SDKs/MacOSX10.4u.sdk"
	env.Append(
		CCFLAGS = ['-arch', 'ppc', '-arch', 'i386', '-isysroot', SDK],
		LINKFLAGS = ['-arch', 'ppc', '-arch', 'i386', '-isysroot', SDK, '-Wl,-syslibroot,' + SDK, '-mmacosx-version-min=10.4'],
	)

src = [
	Glob('ext/*.c'),
]

ext = env.LoadableModule(
	'chipmunk', 
	src,
	LIBS=['chipmunk', RBConfig('LIBRUBYARG_SHARED')],
	LIBPATH='..',
)

if(platform.system() == "Darwin"):
	env.Command('chipmunk.bundle', 'chipmunk', Move("$TARGET", "$SOURCE"))
elif(platform.system() == "Linux"):
	env.Command('chipmunk.so', 'libchipmunk.so', Move("$TARGET", "$SOURCE"))


#docs = env.Command(
#	target = 'doc/',
#	source = 'doc_dummy.rb',
#	action = 'cd ruby; rdoc doc_dummy.rb',
#)

Alias('.', [ext])
